define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/index',
                    add_url: 'coupon/add',
                    edit_url: 'coupon/edit',
                    del_url: 'coupon/del',
                    multi_url: 'coupon/multi',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'coupon_id',
                sortName: 'coupon_id',
                columns: [
                    [
                        {field: 'coupon_id', title: __('Coupon_id'), operate: false},
                        {field: 'coupon_name', title: __('Coupon_name'), operate: 'LIKE'},
                        {field: 'par_value', title: __('Par_value'), operate: false},
                        {field: 'use_condition', title: __('Use_condition'), formatter: Controller.api.formatter.denomination, operate: false},
                        {field: 'range', title: __('Range'), formatter: Controller.api.formatter.range, operate: false},
                        {field: 'receive_type_text', title: __('Receive_type'), formatter: Controller.api.formatter.receive, operate: false},
                        {field: 'expiry', title: __('Expiry'), formatter: Controller.api.formatter.expiry, operate: false},
                        {field: 'use_status', title: __('已使用/已领取/发放量'), formatter: Controller.api.formatter.use_status, operate: false},
                        {field: 'starttime', title: __('发放时间'), formatter: Controller.api.formatter.starttime, operate: false},
                        {field: 'status_text', title: __('Status'), operate: false},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            formatter: Controller.api.formatter.custom
                        },
                    ]
                ]
            });

            $(table).on("click", "[data-id].btn-jsff", function (e) {
                e.preventDefault();
                var id = $(this).data("id");

                var that = this;
                Layer.confirm(
                        __('确定结束发放吗？'),
                        {icon: 3, title: __('确定结束发放吗？'), shadeClose: true},
                        function (index) {
                            $.post('coupon/stop', {ids: id}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });

            $(table).on("click", "[data-id].btn-fzdz", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                $('#copyInput').val(id);
                var js = document.getElementById("copyInput");
                js.select();
                var res = document.execCommand("Copy", true);
                console.log(res);
//                window.clipboardData.setData("Text",'123456666');
                Toastr.success('复制成功');
            });



            $(table).on("click", "[data-id].btn-ffhy", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.prompt({title: __('请输入接受优惠券的用户ID，多个用户用英文逗号,隔开'), formType: 2}, function (value, index) {
                    $.post(url, {id: id, user_ids: value}, function (res) {
                        if (res.code == 0) {
                            Toastr.error(res.msg);
                        } else {
                            table.bootstrapTable('refresh', {});
                        }
                    });
                    Layer.close(index);
                });

            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $("#c-range").change(function () {
                    var val = $(this).val();

                    if (val == 0) {
                        $('#js-brand').hide();
                        $('#js-cate').hide();
                    }

                    if (val == 1) {
                        $('#js-brand').show();
                        $('#js-cate').hide();
                    }

                    if (val == 2) {
                        $('#js-brand').hide();
                        $('#js-cate').show();
                    }
                });

                $("#c-expiry_type").change(function () {

                    var val = $(this).val();

                    if (val == 0) {
                        $('#expiryType0').show();
                        $('#expiryType1').hide();
                    }

                    if (val == 1) {
                        $('#expiryType0').hide();
                        $('#expiryType1').show();
                    }

                });

                $("#c-receive_type").change(function () {

                    var val = $(this).val();
                    if (val == 2) {
                        $('#receive-conf').show();
                    } else {
                        $('#receive-conf').hide();
                    }

                });


                $(".js-show-brand-ck").bind("click", function () {
                    var show_text = $(this).children('.js-show-txt').text();
                    if (show_text == '展开') {
                        $(this).children('.js-show-txt').text('收起');
                    } else {
                        $(this).children('.js-show-txt').text('展开');
                    }
                    $(".js-show-brand-list").slideToggle();
                });
                $(".js-show-cate-ck").bind("click", function () {
                    var show_text = $(this).children('.js-show-txt').text();
                    if (show_text == '展开') {
                        $(this).children('.js-show-txt').text('收起');
                    } else {
                        $(this).children('.js-show-txt').text('展开');
                    }
                    $(".js-show-cate-list").slideToggle();
                });



            },
            formatter: {
                denomination: function (value, row, index) {
                    return '满' + row.use_condition + '可用';
                },
                expiry: function (value, row, index) {
                    if (row.expiry_type == 0) {
                        return row.expiry + '天';
                    }

                    if (row.expiry_type == 1) {
                        return row.expiry_start + '<br>-<br>' + row.expiry_end;
                    }
                },
                receive: function (value, row, index) {
                    if (row.receive_type != 2) {
                        return row.receive_type_text;
                    } else {
                        return row.receive_type_text + '<br><div class="text-info">累计消费:￥' + row.receive_conf + '</div>';
                    }
                },
                starttime: function (value, row, index) {
                    return row.starttime + '<br>-<br>' + row.endtime;
                },
                use_status: function (value, row, index) {
                    return row.used + '/' + row.received + '/' + row.circulation;
                },
                publish_time: function (value, row, index) {
                    return row.starttime_text + '<br>' + row.endtime_text;
                },
                range: function (value, row, index) {

                    if (row.range == 0) {
                        return '全场通用';
                    }

                    if (row.range == 1) {
                        return '品牌优惠券<br>' + '<a href="coupon/show/ids/' + row.coupon_id + '" class="text-success btn-dialog" title="适用品牌" >适用品牌</a>';
                    }

                    if (row.range == 2) {
                        return '品类优惠券<br>' + '<a href="coupon/show/ids/' + row.coupon_id + '" class="text-success btn-dialog" title="适用品类" >适用品类</a>';
                    }
                },
                custom: function (value, row, index) {
                    if (row.status == 0) {
                        var html = '<a href="coupon/edit/ids/' + row.coupon_id + '" class="text-success btn-dialog" title="编辑" data-table-id="table">编辑</a>';
                        return html;
                    } else if (row.status == 1) {
                        var html = '<a href="javascript:;" class="text-danger btn-jsff" data-id="' + row.coupon_id + '">结束发放</a>';

                        if (row.receive_type == 0) {
                            html += '|<a href="javascript:;" class="text-info btn-ffhy" data-id="' + row.coupon_id + '" data-url="coupon/senduser">发放给指定用户</a>';
                        }

                        if (row.receive_type == 1) {
                            html += '|<a href="javascript:;" class="text-info btn-fzdz" data-id="' + row.coupon_id + '">复制领取链接</a>';
                        }
                        return html;
                    } else if (row.status == 10) {
                        return '-';
                    }

                }
            },
        }
    };
    return Controller;
});